﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Data;

namespace Carriage
{
    public class DHLClass
    {
        private string path = System.Web.HttpContext.Current.Server.MapPath("~/DHLPrice.xml");

        private double _weight = 0;
        /// <summary>
        /// 货物净重(单位:克) 
        /// </summary>
        internal double weight
        {
            get
            {
                return _weight;
            }
            set
            {
                _weight = value;
            }
        }

        private double _weightrate = 1.3;
        /// <summary>
        /// 毛重与净重的比率
        /// </summary>
        public double weightrate
        {
            get
            {
                return _weightrate;
            }
            set
            {
                _weightrate = value;
            }
        }

        private int _areaNumber;
        /// <summary>
        /// 所在ｄｈｌ分布区号
        /// </summary>
        public int AreaNumber
        {
            get
            {
                return _areaNumber;
            }
            set
            {
                _areaNumber = value;
            }
        }

        private double _Fuelcosts = 1.18;
        /// <summary>
        /// 燃油费系数
        /// </summary>
        public double Fuelcosts
        {
            get
            {
                return _Fuelcosts;
            }
            set
            {
                _Fuelcosts = value;
            }
        }

        private double _Surcharge = 50;
        /// <summary>
        /// 附加费
        /// </summary>
        public double Surcharge
        {
            get
            {
                return _Surcharge;
            }
            set
            {
                _Surcharge = value;
            }
        }


        /// <summary>
        /// 获取dhl的运费
        /// 所需条件:
        /// １.目的国家所在ｄｈｌ分布区区号
        /// ２.重量
        /// ３.读取所在区的价格信息
        /// </summary>
        /// <returns></returns>
        internal double GetCarriage()
        {
            double rlt = 0;
            double value1 = 0;//起重标准
            double value2 = 0;//续重标准
            double value3 = 1;//折扣

            int flag = 1;//计费方式.　１代表按0.5公斤算,２代表用ＫＧ算
            double cw = (double)(this.weight * this.weightrate);//货物的毛重
            //cw = double.Round(cw,2);
            double cwKg = cw / 1000;
            //cwKg = double.Round(cwKg,2);

            XmlDataDocument dhldata = new XmlDataDocument();//新建一个ｘｍｌ数据文档对象
            dhldata.DataSet.ReadXml(path);
            DataView objDv = dhldata.DataSet.Tables[0].DefaultView;
            objDv.Sort = "Endweight asc";//排行方法
            objDv.RowFilter = "NO=" + AreaNumber.ToString().Trim() + " and Endweight>=" + cwKg.ToString().Trim();//只取本区价格Beginweight　Endweight

            double maxvalue = 0;
            DataRowView maxrowview = objDv[0];
            foreach (DataRowView rowview in objDv)
            {
                if (double.Parse(rowview["Endweight"].ToString().Trim()) <= double.Parse(maxrowview["Endweight"].ToString().Trim()))
                {
                    maxrowview = rowview;
                }
            }

            if (objDv.Count > 0)
            {
                //获取首重和续重 以及计费方式
                value1 = double.Parse(maxrowview["Begincost"].ToString().Trim());
                value2 = double.Parse(maxrowview["Extracost"].ToString().Trim());
                flag = int.Parse(maxrowview["Flag"].ToString().Trim());
                value3 = double.Parse(maxrowview["Discount"].ToString().Trim());

                dhldata = null;//清空对XML数据的操作

                if (flag == 1)
                {
                    //按0.5kg计算
                    if (cw <= 500)
                        rlt = value1;
                    else
                        rlt = value1 + ((cw - 500) / 500) * value2;
                }
                else if (flag == 2)
                {
                    int unit = 0;
                    //按kg计算
                    double rvalue = cwKg % 1;
                    if (rvalue > 0)
                        unit = (int)cwKg + 1;
                    else
                        unit = (int)cwKg;
                    rlt = unit * value1;
                }

                return rlt * value3 * _Fuelcosts + _Surcharge;//标准运费乘折扣
            }
            else
                return 0;
        }
    }
}
